package com.apps.file.service;


import com.apps.sutils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * 文件扫描
 */
@Slf4j
public class FileHelper
{


    /**
     * TODO:递归扫描指定文件夹下面的指定文件
     * @return ArrayList<Object>
     * @throws FileNotFoundException
     */
    public static void scanFilesWithRecursion(String folderPath) throws FileNotFoundException
    {
        File directory = new File(folderPath);
        if(!directory.isDirectory())
        {
            throw new FileNotFoundException('"' + folderPath + '"' + " input path is not a Directory , please input the right path of the Directory. ^_^...^_^");
        }

        if(directory.isDirectory())
        {
            File [] filelist = directory.listFiles();
            for(int i = 0; i < filelist.length; i ++)
            {
                /**如果当前是文件夹，进入递归扫描文件夹**/
                if(filelist[i].isDirectory())
                {
                    /**递归扫描下面的文件夹**/
                    scanFilesWithRecursion(filelist[i].getAbsolutePath());
                }
                /**非文件夹**/
                else
                {
                    //处理文件
                }
            }
        }

    }

    /**
     * 无递归扫描指定文件夹下面的指定后缀文件
     * @param folderPath
     * @return
     * @throws FileNotFoundException
     */
    public static List<String> scanFilesWithNoRecursion(String folderPath,boolean all,String... ext) throws FileNotFoundException
    {
        /**
         * 结果集
         */
        List<String> scanFiles = new ArrayList<>();

        /**待扫描文件夹*/
        LinkedList<File> queueFiles = new LinkedList<File>();

        File directory = new File(folderPath);
        if (!directory.isDirectory())
        {
            throw new FileNotFoundException('"' + folderPath + '"' + " 不是一个有效目录");
        } else
        {
            //首先将第一层目录扫描一遍
            File[] files = directory.listFiles();
            //遍历扫出的文件数组，如果是文件夹，将其放入到linkedList中稍后处理
            for (int i = 0; i < files.length; i++)
            {
                if (files[i].isDirectory())
                {
                    queueFiles.add(files[i]);
                } else
                {
                    if(StringUtils.isContains(ext,files[i].getName().split("\\.")[1]))
                    //暂时将文件名放入scanFiles中
                    scanFiles.add(files[i].getAbsolutePath());
                }
            }

            //如果linkedList非空遍历linkedList
            while (!queueFiles.isEmpty() && all)
            {
                //移出linkedList中的第一个
                File headDirectory = queueFiles.removeFirst();
                File[] currentFiles = headDirectory.listFiles();
                for (int j = 0; j < currentFiles.length; j++)
                {
                    if (currentFiles[j].isDirectory())
                    {
                        //如果仍然是文件夹，将其放入linkedList中
                        queueFiles.add(currentFiles[j]);
                    } else
                    {
                        if(StringUtils.isContains(ext,currentFiles[j].getName().split("\\.")[1]))
                           scanFiles.add(currentFiles[j].getAbsolutePath());
                    }
                }
            }
        }

        return scanFiles;
    }

    /**
     * 读取txt文件
     * @param filePath
     * @return
     */
    public static List<String> readTxtFile(String filePath)
    {
        List<String> result=new ArrayList<>();
        try {
            String encoding="UTF-8";
            File file=new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while((lineTxt = bufferedReader.readLine()) != null){
                    result.add(lineTxt);
                }
                read.close();
            }else{
                log.info("找不到指定的文件");
            }
        } catch (Exception e) {
            log.info("读取文件内容出错");
            e.printStackTrace();
        }

      return result;
    }

    public static void main(String... args)
    {
           List<String> jsons=readTxtFile("E:\\python-workspace\\kupa8\\file\\tukuppt.txt");
           for(int i=0;i<10;i++)
               System.out.println(jsons.get(i));
    }
}
